package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;

import com.badibadi.infos.Results;
import com.badibadi.infos.TypeId;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.id;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.libjph.util.StringUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

/**
 * 添加分组
 * 
 * @author Administrator
 * 
 */
public class AddAGroupActivity extends BaseActivity {
	Intent intent = null;
	private Results results2, results3;
	private EditText add_group_name;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(AddAGroupActivity.this);
				Utils.showMessage(AddAGroupActivity.this, getResources()
						.getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(AddAGroupActivity.this);
				// Utils.showMessage(AddAGroupActivity.this,
				// results2.getRetmsg());
//				   
				if (results2.isRet()) {
					Intent intent = new Intent(AddAGroupActivity.this,
							ChoiceCrewActivity.class);
					// startActivity(intent);
					startActivityForResult(intent, 21);
					// finish();
				} else {
					if (results2.getRetmsg().equals("该分组已存在，请重新命名！")) {
						Utils.showMessage(AddAGroupActivity.this,
								getResources().getString(R.string.wx_txt_118));
					}

				}
				break;
			case 3:
				Utils.ExitPrgress(AddAGroupActivity.this);
				Utils.showMessage(AddAGroupActivity.this, getResources()
						.getString(R.string.l_xa10));
				break;
			case 5:
				Utils.ExitPrgress(AddAGroupActivity.this);
				Utils.showMessage(AddAGroupActivity.this, results3.getRetmsg());
				if (results3.isRet()) {
					// Intent intent = new Intent(AddAGroupActivity.this,
					// ChoiceCrewActivity.class);
					// startActivity(intent);
					// startActivityForResult(intent, 21);
					finish();
				}
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_add_agroup);
		add_group_name = (EditText) findViewById(R.id.add_group_name);

		this.findViewById(R.id.Save).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String name = add_group_name.getText().toString().trim();
				if (name != null) {
					createGroup(name);
				} else {
					Utils.showMessage(AddAGroupActivity.this, getResources()
							.getString(R.string.l_xb17));
				}
				// intent = new Intent(AddAGroupActivity.this,
				// ChoiceCrewActivity.class);
				// startActivity(intent);
			}
		});
		this.findViewById(R.id.back_button).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == 21 && arg1 == 21) {
			String result = arg2.getStringExtra("res");
			if (!StringUtil.isNullOrEmpty(result)) {
//				   
				moveFriend(results2.getRetmsg(), result);
			}
		} else {
			finish();
		}
	}

	private void createGroup(final String name) {
		Utils.showPrgress(AddAGroupActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(AddAGroupActivity.this));
				map.put("name", name);
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "chat/createGroup");
				if (content != null) {
					results2 = Utils.checkResult_NNN(getApplicationContext(),
							content);
					//    ("添加分组返回参数是:" + content);
//					   
					if (results2 != null
							&& !results2.getRetmsg().equals("null")) {
						handler.sendEmptyMessage(2);
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	private void moveFriend(final String groupid, final String fids) {
		Utils.showPrgress(AddAGroupActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(AddAGroupActivity.this));
				map.put("fid", fids);
				map.put("groupId", groupid);
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "chat/moveFriend");
				if (content != null) {
					results3 = Utils.checkResult_NNN(getApplicationContext(),
							content);
					//    ("好友分组返回参数是:" + content);
					if (results3 != null
							&& !results3.getRetmsg().equals("null")
							&& results3.isRet()) {
						handler.sendEmptyMessage(5);
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

}
